package A2;

import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Vector;
import java.awt.event.ActionListener;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;

/**
 * <p>Title: Class to handle Store Manager GUI</p>
 * <p>Description: CS 343 Assignment #2</p>
 * @author Mike Harmon
 * @author Erik Olson
 * @author Casey Golla
 * @author Jack Freiermuth
 * @email michael.harmon@my.uwrf.edu
 * @email erik.olson@my.uwrf.edu
 * @email casey.golla@my.uwrf.edu
 * @email jack.freiermuth@my.uwrf.edu
 * @date November 4th 2013
 * @team 5
 */ 
public class StoreManager extends JFrame {

	private static List<OrderOfWidgets> orders;
	private static Vector<String> completableOrders = new Vector<String>();
	private static Vector<String> noncompletableOrders = new Vector<String>();
	private JList<String> listView;
	private JScrollPane scrollPane;
	private static Vector<String> OrderString = new Vector<String>();
	private JRadioButton all_orders;
	private JRadioButton completeable_orders;
	private JRadioButton non_completable_orders;
	private static List<Widget> widgets;
	private static int widget_1_quantity = 0;
	private static int widget_2_quantity = 0;
	private static int widget_3_quantity = 0;
	private static int widget_4_quantity = 0;
	private static int widget_5_quantity = 0;
	private static int widget_6_quantity = 0;
	private static int widget_7_quantity = 0;
	private static int widget_8_quantity = 0;
	private static int widget_9_quantity = 0;
	private static int widget_a_quantity = 0;
	
	public StoreManager() {
		//setting bounds of window
		setBounds( 5, 5, 1275, 220 );
		setLayout( null );
		
		/*
		 * Set up a JList object to display a list 
		 */
		listView = new JList<String>( OrderString );
		
		//adding scroll bars to list
		scrollPane = new JScrollPane( listView );
		scrollPane.setBounds(5, 5, 1090, 175);
		add( scrollPane );
		
		//adding an "All" radio button
		all_orders = new JRadioButton( "All" );
		all_orders.addActionListener( new ActionListener() {
			/*
			 * This method is executed each time the radio button is clicked.
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(ActionEvent e) {
				//swap list to display all orders
				listView.setListData( OrderString );
			}
		});
		all_orders.setBounds(1103, 5, 165, 25);
		all_orders.setSelected(true);
		add(all_orders);
		
		//adding a "Completeable" radio button
		completeable_orders = new JRadioButton( "Completeable" );
		completeable_orders.addActionListener( new ActionListener() {
			/*
			 * This method is executed each time the b radioutton is clicked.
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(ActionEvent e) {
				//swap list to display all completeable orders
				listView.setListData( completableOrders );
			}
		});
		completeable_orders.setBounds(1103, 50, 165, 25);
		add(completeable_orders);
		
		//adding a "Non-Completeable" radio button
		non_completable_orders = new JRadioButton( "Non-Completeable" );
		non_completable_orders.addActionListener( new ActionListener() {
			/*
			 * This method is executed each time the radio button is clicked.
			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
			 */
			public void actionPerformed(ActionEvent e) {
				//swap list to display all non-completeable orders
				listView.setListData( noncompletableOrders );
			}
		});
		non_completable_orders.setBounds(1103, 95, 165, 25);
		add(non_completable_orders);
		
		//putting the radio buttons in a goup so that only 1 can be selected
		ButtonGroup group = new ButtonGroup();
		group.add(all_orders);
		group.add(completeable_orders);
		group.add(non_completable_orders);
		
		//making the window visible
		setVisible( true );
		setDefaultCloseOperation( EXIT_ON_CLOSE );
	}

	/**
	 * Function to get all orders from the db
	 */
	public static void getOrders() {
		final String PERSISTENCE_UNIT_NAME = "WidgetsAndOrders";
		EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
		
	    EntityManager em = factory.createEntityManager();

	    // create new widget to persist into the db
	    em.getTransaction().begin();
	    	    
	    // Get the entire Inventory of orders from the database
	    Query q = em.createQuery("select o from OrderOfWidgets o");
	    orders = q.getResultList();
	    em.close();
	    
	    //add the toString from each order to a variable
	    for (OrderOfWidgets w : orders) {
	    	OrderString.add(w.toString());
		}
	}

	/**
	 * Function that loops through all orders and determines if they are completeable or not
	 */
	public static void getCompletableNonCompletableOrders() {
		for( OrderOfWidgets o : orders ) {
			//if we have enough widgets for the order then it is completeable
			if( o.getWidget_1_quantity() <= widget_1_quantity && o.getWidget_2_quantity() <= widget_2_quantity && o.getWidget_3_quantity() <= widget_3_quantity &&
				o.getWidget_4_quantity() <= widget_4_quantity && o.getWidget_5_quantity() <= widget_5_quantity && o.getWidget_6_quantity() <= widget_6_quantity &&
				o.getWidget_7_quantity() <= widget_7_quantity && o.getWidget_8_quantity() <= widget_8_quantity && o.getWidget_9_quantity() <= widget_9_quantity && o.getWidget_a_quantity() <= widget_a_quantity ) {
				widget_1_quantity -= o.getWidget_1_quantity();
				widget_2_quantity -= o.getWidget_2_quantity();
				widget_3_quantity -= o.getWidget_3_quantity();
				widget_4_quantity -= o.getWidget_4_quantity();
				widget_5_quantity -= o.getWidget_5_quantity();
				widget_6_quantity -= o.getWidget_6_quantity();
				widget_7_quantity -= o.getWidget_7_quantity();
				widget_8_quantity -= o.getWidget_8_quantity();
				widget_9_quantity -= o.getWidget_9_quantity();
				widget_a_quantity -= o.getWidget_a_quantity();
				completableOrders.add( o.toString() );
			} else{
				noncompletableOrders.add( o.toString() );
			}
		}
	}

	/**
	 * Function that grabs all widgets from the db
	 */
	public static void getWidgets() {
		 final String PERSISTENCE_UNIT_NAME = "WidgetsAndOrders";
		EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
		
	    EntityManager em = factory.createEntityManager();

	    // create new widget to persist into the db
	    em.getTransaction().begin();
	    	    
	    // Get the entire Inventory of Widgets from the database
	    Query q = em.createQuery("select w from Widget w");
	    widgets = q.getResultList();
	    em.close();
	}
	
	/**
	 * Function to determine how many of each widget we have
	 */
	public static void getQuantitys() {
		for( Widget w : widgets ) {
			if(  w.getName().equals( "Widget 1" ) ) {
				widget_1_quantity++;
			} else if( w.getName().equals( "Widget 2" ) ) {
				widget_2_quantity++;
			} else if( w.getName().equals( "Widget 3" ) ) {
				widget_3_quantity++;
			} else if( w.getName().equals( "Widget 4" ) ) {
				widget_4_quantity++;
			} else if( w.getName().equals( "Widget 5" ) ) {
				widget_5_quantity++;
			} else if( w.getName().equals( "Widget 6" ) ) {
				widget_6_quantity++;
			} else if( w.getName().equals( "Widget 7" ) ) {
				widget_7_quantity++;
			} else if( w.getName().equals( "Widget 8" ) ) {
				widget_8_quantity++;
			} else if( w.getName().equals( "Widget 9" ) ) {
				widget_9_quantity++;
			} else if( w.getName().equals( "Widget a" ) ) {
				widget_a_quantity++;
			}
		}
	}
	
	public static void main( String[] args ) {
		//get all orders
		getOrders();
		
		//get all widgets
		getWidgets();
		
		//determine how many of each widget we have
		getQuantitys();
		
		//determine which orders can be completed
		getCompletableNonCompletableOrders();
		
		//launching view
		new StoreManager();
	}

}